﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台组织机构 -> 职位管理维护页面
    /// Author: tim 2010-02-23
    /// </summary>
    public partial class sys_pos_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : "1";
                _hidPosId.Value = (null != Request.QueryString["posId"]) ? Request.QueryString["posId"].ToString() : string.Empty;
                initializePage();
            }
        }

        private void initializePage()
        {
            PosBLL posBLL = new PosBLL();
            string posId = _hidPosId.Value;
            
            initAddNew();
            MsPos msPos = posBLL.GetPosInfo(posId);                        
            initEdit(msPos);

            msPos = null;
            posBLL.Release();
            posBLL = null;
        }

        private void initAddNew()
        {
            txtPosName.Text = string.Empty;
            txtPosCode.Text = string.Empty;
            txtPosDesc.Text = string.Empty;
            _hidSelUser.Value = string.Empty;
            txtSelUser.Text = string.Empty;
            txtPosOrder.Text = string.Empty;
        }

        private void initEdit(MsPos msPos)
        {
            if (null == msPos) return;

            txtPosName.Text = msPos.posName;
            txtPosCode.Text = msPos.posCode;
            txtPosDesc.Text = msPos.posDesc;
            _hidSelUser.Value = msPos.includeUsers.key;
            txtSelUser.Text = msPos.includeUsers.val.Trim().Replace(" ","\r\n");
            txtPosOrder.Text = msPos.orderNo;
        }


        #region ajax methods

        /// <summary>
        /// 校验是否存在同名职位
        /// </summary>
        /// <param name="posId"></param>
        /// <param name="posName"></param>
        /// <param name="organId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string CheckPosNameExist(string posId, string posName, string organId)
        {
            PosBLL posBLL = new PosBLL();
            List<string> lstResult = posBLL.IsExistPosName(posName, posId, organId);
            var newObj = new {
                OPERTYPE = "POSNAMECHECK",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            posBLL.Release();
            posBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 保存职位信息
        /// </summary>
        /// <param name="strXML"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SavePosData(string jsonExpr,string ctrlType)
        {
            MsPos msPos = JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsPos)) as MsPos;
            PosBLL posBLL = new PosBLL();

            List<string> lstResult = posBLL.Save(msPos);
            var newObj = new {
                OPERTYPE = "POSSAVE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                CTRLTYPE = ctrlType
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            posBLL.Release();
            posBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="posId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeletePos(string posId)
        {
            PosBLL posBLL = new PosBLL();
            List<string> lstResult = posBLL.Delete(posId);
            var newObj = new {
                OPERTYPE = "POSDELETE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            posBLL.Release();
            posBLL = null;
            return serExpr;
        }

        #endregion
    }
}
